<?php

namespace app\admin\controller\member;

use app\common\controller\Backend;
use think\Db;
/**
 * 用户节点管理
 *
 * @icon fa fa-circle-o
 */
class Usertree extends Backend
{
    
    /**
     * Usertree模型对象
     * @var \app\admin\model\member\Usertree
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\member\Usertree;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function index()
    {
        $one = db('users')->where('pid',0)->value('mobile');
        if ($this->request->isAjax()) {
            $account = input('param.account');
            $type = input();
            $data = [];
            if (null !== $account) {
                $own = db('users')->where('mobile', $account)->value('id');
                #获取直推用户的id集合
                $childId = db('users')->where('pid', $own)->column('id');
                if (isset($type['one'])){
                    $childId = $own;
                }
                if ($childId) {
                    $list = db('users')
                        ->where(['id'=>['in',$childId]])
                        ->field('id,mobile,truename,level')
                        ->select();

                    if ($list) {
                        foreach ($list as $k => $v) {
                            $team_num=0;
                            $users_position=Db::name('users_position')->where(['pid'=>$v['id']])->select();
                            $list[$k]['team_count'] = count($users_position)+1;
                            $list[$k]['truename']=($v['truename']==''||$v['truename']==null)?'-':$v['truename'];
//                            $team_num = Db::name('users_position')
//                                ->alias('p')
//                                ->join('users u','u.id=p.user_id','LEFT')
//                                ->where(['p.pid'=>$v['id']])
//                                ->sum('balance');
//                            $list[$k]['team_num'] =$team_num+Db::name('users')->where(['id'=>$v['id']])->value('balance');
                            $list[$k]['user_num'] = Db::name('users_position')->where(['lay'=>1,'pid'=>$v['id']])->count();
                            $model = new \app\admin\model\Users;
//                            Db::name('users_position')->where(['pid'=>])->select();
//                            if (!empty($users_position)){
//                                $list[$k]['level'] = $model->getLevelList()[$users_position[0]['level']];
//                            }else{
                            $list[$k]['level'] = $model->getLevelList()[$v['level']];
//                            }
                        }
                    }
                } else {
                    $list = [];
                }
                $data['child'] = $list;
                $this->success('', '', $data);
            }
            $this->success('', '', []);

        }
        $this->assign('one',$one);
        return $this->view->fetch();
    }

}
